/**
 *
 */
package com.longrise.android.byjk.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.util.Util;

/**
 * simple introduction
 * <p>
 * <p>
 * detailed comment
 *
 * @author xkj 2015-12-25
 * @see
 * @since 1.0
 */
public class InputBoxView extends LinearLayout implements OnClickListener, TextWatcher, View.OnFocusChangeListener {

    public static final int NORMAL = 0;

    public static final int PASSWORD = 1;

    public static final int SHOWPOPUP = 3;

    public static final int USERIF = 4; //叉叉  点击删除

    public static final int POPUP = 2;

    private int mRightimagemode = 1;

    public static final String TAG = "ImputBoxView";

    private Context mContext;

    private Drawable mDrawable_left;

    private Drawable mDrawable_right;

    private String mHint;

    private EditText mEt;

    private ImageView mIv_right;

    private ImageView mIv_left;

    private int mMode;

    private Drawable mDrawable_right2;

    private ListAdapter mListAdapter;

    private PopupWindow pw;

    private RelativeLayout mRl_right;
    private Drawable mDrawable_left2;

    /**
     * @param context
     * @param attrs
     */
    public InputBoxView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * @param context
     */
    public InputBoxView(Context context) {
        this(context, null);
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public InputBoxView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        TypedArray ta = mContext.getTheme().obtainStyledAttributes(attrs,
                R.styleable.input_box, defStyle, 0);
        int n = ta.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = ta.getIndex(i);
            switch (attr) {
                case R.styleable.input_box_left_pic:
                    mDrawable_left = ta.getDrawable(attr);
                    break;
                case R.styleable.input_box_left_pic2:
                    mDrawable_left2 = ta.getDrawable(attr);
                    break;
                case R.styleable.input_box_right_pic:
                    mDrawable_right = ta.getDrawable(attr);
                    break;
                case R.styleable.input_box_right_pic2:
                    mDrawable_right2 = ta.getDrawable(attr);
                    break;
                case R.styleable.input_box_hint:
                    mHint = ta.getString(attr);
                    break;
                case R.styleable.input_box_text_mode:
                    mMode = ta.getInt(attr, 0);
                    break;

                default:
                    break;
            }
        }
        initView();

    }

    public void initView() {
        View mView = View.inflate(mContext, R.layout.input_box, null);
        mIv_left = (ImageView) mView.findViewById(R.id.imput_pic1);
        mIv_right = (ImageView) mView.findViewById(R.id.imput_pic2);
        mRl_right = (RelativeLayout) mView.findViewById(R.id.imput_right_rl);
        mEt = (EditText) mView.findViewById(R.id.imput_text);
        if (null != mDrawable_left) {
            mIv_left.setImageDrawable(mDrawable_left);
        } else {
            mIv_left.setVisibility(View.GONE);
        }
        if (null != mDrawable_right) {
            mIv_right.setImageDrawable(mDrawable_right);
        } else {
            mIv_right.setVisibility(View.GONE);
        }

        if (null != mHint) {
            mEt.setHint(mHint);
        } else {
            mEt.setHint(null);
        }

        if (mMode == InputBoxView.PASSWORD) { // 设置的是密码模式的话
            setTextMode(PASSWORD);
            mEt.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        }

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        addView(mView, params);

        mEt.addTextChangedListener(this);
        mEt.setOnFocusChangeListener(this);
    }

    public EditText getEditText() {
        return mEt;
    }

    public void setText(String text) {
        mEt.setText(text);
    }

    public String getText() {
        if (null != mEt) {
            return mEt.getText().toString().trim();
        } else {
            return null;
        }
    }

    public void setTextMode(int mode) {
        switch (mode) {
            case NORMAL: // 正常显示
                // mEt.setInputType(InputType.TYPE_CLASS_TEXT);
                mMode = NORMAL;
                mEt.setTransformationMethod(HideReturnsTransformationMethod
                        .getInstance());
                mEt.setSelection(mEt.length());
                if (null != mDrawable_right) {
                    mIv_right.setImageDrawable(mDrawable_right);
                }
                break;
            case PASSWORD: // 密码的方式
                // mEt.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                mMode = PASSWORD;
                mEt.setTransformationMethod(PasswordTransformationMethod
                        .getInstance());
                mEt.setSelection(mEt.length());
                if (null != mDrawable_right) {
                    mIv_right.setImageDrawable(mDrawable_right2);
                }
                break;
            default:
                break;
        }
    }

    /**
     * 设置右边图片的点击事件 同事判断是显示密码 还是显示popup
     *
     * @param mode
     */
    public void setRightClickMode(int mode) {
        this.mRightimagemode = mode;
        if (null != mDrawable_right) {
            mIv_right.setOnClickListener(this);
            mRl_right.setOnClickListener(this);
        }

        if (USERIF == mode) {  //叉叉
            mIv_right.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.imput_pic2:
            case R.id.imput_right_rl:
                if (mRightimagemode == PASSWORD) { // 右边图片为显示密码
                    switchTextMode();
                } else if (mRightimagemode == USERIF) { // 右边图面是显示popup
                    getEditText().setText("");
                }

                break;

            default:
                break;
        }

    }

    /**
     * 切换edittext的输入方式
     */
    public void switchTextMode() {
        if (mMode == NORMAL) {
            setTextMode(PASSWORD);
            mMode = PASSWORD;
        } else {
            setTextMode(NORMAL);
            mMode = NORMAL;
        }

    }


    /**
     * 切换是否显示popupwindow
     */
    public void switchPopup() {
        if (mMode == NORMAL) {
            showPopup(); // 显示popu
            if (null != mDrawable_right) {
                mIv_right.setImageDrawable(mDrawable_right2); // popup出现后需要显示的图片
            }
            mMode = SHOWPOPUP; // 模式设置为显示popup
        } else {
            dismisspw(); // 取消popu
            if (null != mDrawable_right) {
                mIv_right.setImageDrawable(mDrawable_right); // 显示默认的图片
            }

            mMode = NORMAL;

        }

    }

    public void setPopupAdapter(ListAdapter adapter) {
        this.mListAdapter = adapter;
    }

    /**
     * 显示popupwindow
     */
    public void showPopup() {
        View contentView = View.inflate(mContext, R.layout.item_popup, null);
        ListView lv = (ListView) contentView.findViewById(R.id.item_popup_lv);

        lv.setAdapter(mListAdapter);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                                    int position, long arg3) {
                LinearLayout mView = (LinearLayout) view;
                TextView tv = (TextView) mView
                        .findViewById(R.id.call_item_tztg_tv);
                String text = tv.getText().toString().trim();
                mEt.setText(text);
                dismisspw();
                mMode = NORMAL;
                mIv_right.setImageDrawable(mDrawable_right); // 显示默认图片
            }

        });

        int width = mEt.getWidth();
        int height = mEt.getHeight();
        pw = new PopupWindow(contentView, width, Util.dip2px(mContext, 100));
        pw.setOutsideTouchable(false);
        pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        pw.showAsDropDown(mEt);
        pw.setFocusable(true);

    }

    /**
     * 控制右边图片是否显示
     * 一般不用此方法
     */
    public void setRightIcVisible(Boolean icVisible){
        if(icVisible){
            mIv_right.setVisibility(View.VISIBLE);
            mIv_right.setClickable(true);
            mRl_right.setClickable(true);
        }else{
            mIv_right.setVisibility(View.INVISIBLE);
            mIv_right.setClickable(false);
            mRl_right.setClickable(false);
        }
    }


    /**
     * 隐藏pw
     */
    public void dismisspw() {
        if (pw != null && pw.isShowing()) {
            pw.dismiss();
            pw = null;
        }
    }

    //----------------------------addTextChangedListener----------------------------
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String text = mEt.getText().toString().trim();
        if (TextUtils.isEmpty(text)) {
            mIv_left.setImageDrawable(mDrawable_left);
            mIv_right.setVisibility(View.INVISIBLE);
            mIv_right.setClickable(false);
            mRl_right.setClickable(false);
        } else {
            mIv_left.setImageDrawable(mDrawable_left2);
            mIv_right.setVisibility(View.VISIBLE);
            mIv_right.setClickable(true);
            mRl_right.setClickable(true);
        }
    }

    //----------------------------addTextChangedListener----------------------------
    //-----------------------------setOnFocusChangeListener-------------------------
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus && (!TextUtils.isEmpty(mEt.getText()))) {
            mIv_right.setVisibility(View.VISIBLE);
            mIv_right.setClickable(true);
            mRl_right.setClickable(true);
        } else {
            mIv_right.setVisibility(View.INVISIBLE);
            mIv_right.setClickable(false);
            mRl_right.setClickable(false);
        }
    }
    //-----------------------------setOnFocusChangeListener-------------------------


}
